/**
 * Created by perqin on 16-12-6.
 */

'use strict';

var http = require('http'),
  photosPolicy = require('../policies/photos.server.policy'),
  photos = require('../controllers/photos.api.server.controller');

module.exports = function (app, proxy) {
  // Photos upload and download in story
  app.route('/api/stories/:storyId/photos/upload').all(photosPolicy.isAllowed).post(function (req, res, next) {
    req.url = '/upload';
    proxy.web(req, res);
  });
  app.route('/api/stories/:storyId/photos/upload/:uploadId').all(photosPolicy.isAllowed).put(function (req, res, next) {
    req.url = '/upload' + req.params.uploadId;
    proxy.web(req, res);
  });
  app.route('/api/stories/:storyId/photos').all(photosPolicy.isAllowed).post(photos.addUploadedPhotosToStory);
  app.route('/api/files/:filename').all(photosPolicy.isAllowed).get(function (req, res, next) {
    // Remark: I don't know any other choice but to modify req.url
    req.url = '/' + req.params.filename;
    proxy.web(req, res);
  });

  // existed photo operate
  app.route('/api/photos/:photoId').all(photosPolicy.isAllowed)
    .get(photos.get)
    .delete(photos.remove);
  app.route('/api/stories/:storyId/photos').all(photosPolicy.isAllowed).get(photos.listInStory);
  app.route('/api/stories/:storyId/photos/:photoId').all(photosPolicy.isAllowed)
    .get(photos.getInStory)
    .delete(photos.removeInStory);

  // Finish by binding the photo middleware
  app.param('photoId', photos.photoById);
};
